#!/bin/sh

#get version
#VERSION=`cat package.yaml | grep version | sed 's/[[:space:]]//g' | awk -F"[:#]" '{print $2}'`
VERSION="V7.3.2"

#pacakge base info
yoc show -c > comp_list.tmp

#get board
BOARD_NAME=`cat comp_list.tmp | grep "board component" |  awk -F" " '{print $4}'`
CHIP_NAME=`cat comp_list.tmp | grep "chip component" |  awk -F" " '{print $4}'`

#block first
#cat project.cdkproj | awk 'BEGIN{pt=1}{if(match($0,"<Package ID")){pt=0};if(pt==1){print $0}}' > 1.tmp
cat project.cdkproj |  grep -v "</Project>" > 1.tmp

#package list
echo "  <Packages>" > 2.tmp
cat comp_list.tmp | grep "\* " |awk -F' ' '{printf "%s\n", $2}' | awk -F':' '{printf "    <Package ID=\"%s\" Version=\"'${VERSION}'\" IsBasic=\"false\"/>\n", $1}' >> 2.tmp
echo "  </Packages>" >> 2.tmp

#package list del chip & board
cat 2.tmp | grep -v "${CHIP_NAME}" | grep -v "${BOARD_NAME}" >> 1.tmp

#append board & chip
#cat project.cdkproj | awk 'BEGIN{pt=0}{if(match($0,"</Packages>")||pt==1){pt=1;print $0}}' >> 1.tmp
echo "  <Boards>" >> 1.tmp
echo "    <Board ID=\"${BOARD_NAME}\" Version=\"${VERSION}\"/>" >> 1.tmp
echo "  </Boards>" >> 1.tmp
echo "  <Chips>" >> 1.tmp
echo "    <Chip ID=\"${CHIP_NAME}\" Version=\"${VERSION}\"/>" >> 1.tmp
echo "  </Chips>" >> 1.tmp

#Project end
echo "</Project>" >> 1.tmp

#cleanup
mv 1.tmp project.cdkproj
rm 2.tmp comp_list.tmp

